//	Interpreter.java

import java.io.*;

/** 
	Description: CS 354

	@author Teresa Cole
        @author Samuel Jenkins
	@version Fall 2003 - modified Fall 2008
*/
public class Interpreter implements Token
{

/** 
	 Main entry point
*/
	static public void main(String[] args)  throws IOException
	{
	Lexeme parseTree;
	Lexer scanner = null;
	
	System.out.println( "Program Interpreter ");
	
	if (args.length==0)
	    {
	    scanner = new Lexer( System.in);
	    System.out.println( "Enter code; use CTRL-D to quit. ");
	    }
	else
	    try
		{
		scanner = new Lexer( new FileInputStream( args[0]));
		}
	    catch (IOException e)
		{
		System.err.println( "Lexer: Error opening file " 
		    + args[0] + "\n" + e);
		System.exit( 1);
		}
	Parser parser = new Parser( scanner);
	Evaluator evaluator = new Evaluator( );

	try
	    {
	    parseTree = parser.statement();
	    //parseTree.printTree( System.out, 0);
	    while ( !scanner.atEOF)
		{
		Lexeme result = evaluator.eval( parseTree);
		System.out.println( "\t" + result);
		parseTree = parser.statement();
		}
	    }
	catch (Exception e)
	    {
	    System.err.println( e);
	    e.printStackTrace();
	    System.exit(1);
	    }
	}	
}
